"""
主机初始化
"""
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.common.exceptions import NoSuchElementException
import time
import logging.config
from conf import settings
from interface.init_interface import config_dict
from lib.common import wait_table, host_row_search

logging.config.dictConfig(settings.LOGGING_DIC)
logger = logging.getLogger("初始化")


def initializing_interface(browser, wait, host):
    if host.sync_status != "未初始化":
        logger.info(f"主机 {host.ip} 不需要初始化")
        return
    if host.connect_status not in ('连接成功',):
        logger.error(f"主机 {host.ip} 存在问题,中止初始化")
        return
    # 初始化
    browser.find_element(By.XPATH, '//*[@id="init"]').click()
    for i in range(6, 5000):
        try:
            if browser.find_element(By.XPATH, f'/html/body/div[{i}]').is_displayed():
                browser.find_element(By.XPATH, f'/html/body/div[{i}]/div[11]/button[2]/span').click()

                browser.switch_to.frame(1)
                for t in range(1000):
                    time.sleep(0.2)
                    text = wait.until(EC.presence_of_element_located((
                        By.XPATH, '/html/body/div[1]/div/div/div/div'
                    ))).text
                    if "初始化完成" in text:
                        print(text)
                        logger.info(f"主机 {host.ip}: 初始化成功")
                        break
                    if "初始化失败" in text:
                        print(text)
                        host.sync_status = "初始化失败"
                        host.is_bad = True
                        logger.error(f"主机 {host.ip}: 初始化失败")
                        break
                    if t == 999:
                        host.sync_status = "状态异常"
                        host.is_bad = True
                        logger.error(f"主机 {host.ip}: 状态异常 一直正在初始化")
                        break
                browser.switch_to.parent_frame()
                browser.find_element(By.XPATH, f'/html/body/div[{i + 1}]/div[1]/a').click()
                break
        except NoSuchElementException as e:
            pass


def only_initializing_interface(browser, host):
    initializing_status = []
    if config_dict.get('ALLOWS_INIT_STATUS') == 2:
        initializing_status.append("未初始化")
    else:
        initializing_status.append("未初始化")
        initializing_status.append("初始化失败")

    host_row = host_row_search(browser, host)

    if host_row is None:
        logger.error(f"主机 {host.ip} 不存在,中止初始化")
        return

    if host.sync_status not in initializing_status:
        host.sync_status += ' 无法初始化'
        host.save()
        return
    host_row.click()

    # 初始化
    browser.find_element(By.XPATH, '//*[@id="init"]').click()
    for i in range(6, 5000):
        try:
            if browser.find_element(By.XPATH, f'/html/body/div[{i}]').is_displayed():
                # time.sleep(1)
                browser.find_element(By.XPATH, f'/html/body/div[{i}]/div[11]/button[2]/span').click()
                browser.switch_to.frame(1)
                # time.sleep(1)
                browser.switch_to.parent_frame()
                browser.find_element(By.XPATH, f'/html/body/div[{i + 1}]/div[1]/a').click()
                host.sync_status = '已自动初始化'
                host.save()
                break
        except NoSuchElementException as e:
            pass

